package com.example.gss.controller;

import com.example.gss.entity.ResearchAchievement;
import com.example.gss.Service.ResearchAchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/research")
public class ResearchAchievementController {

    @Autowired
    private ResearchAchievementService achievementService;

    // 查询所有科研成果
    @GetMapping
    public ResponseEntity<List<ResearchAchievement>> getAll() {
        return new ResponseEntity<>(achievementService.getAll(), HttpStatus.OK);
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public ResponseEntity<ResearchAchievement> getById(@PathVariable String id) {
        ResearchAchievement achievement = achievementService.getById(id);
        return achievement != null ?
                new ResponseEntity<>(achievement, HttpStatus.OK) :
                new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // 根据学生ID查询
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<ResearchAchievement>> getByStudentId(@PathVariable String studentId) {
        return new ResponseEntity<>(achievementService.getByStudentId(studentId), HttpStatus.OK);
    }

    // 添加科研成果
    @PostMapping
    public ResponseEntity<Integer> save(@RequestBody ResearchAchievement achievement) {
        int result = achievementService.save(achievement);
        return result > 0 ?
                new ResponseEntity<>(result, HttpStatus.CREATED) :
                new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    // 更新科研成果
    @PutMapping
    public ResponseEntity<Integer> update(@RequestBody ResearchAchievement achievement) {
        int result = achievementService.update(achievement);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    // 删除科研成果
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable String id) {
        int result = achievementService.delete(id);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}